package com.example.hibernate.program;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.annotations.OnDelete;

public class Driver {

	public static void main(String[] args) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		try {
			// fromClause(session);
			// asClause(session);
			// selectClause(session);
			// whereClause(session);
			// orderByClause(session);
			// groupByClause(session); // TODO
//			parameterBinding(session);
//			paginationQuery(session);
			uniqueValue(session);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		}
		session.close();
		sessionFactory.close();
	}

	private static void fromClause(Session session) {
		// Query query = session.createQuery("FROM Employee");
		Query query = session.createQuery("FROM com.example.hibernate.program.Employee");
		List<Employee> employees = query.list();
		for (Employee employee : employees) {
			System.out.println(employee);
		}
		System.out.println("Total Count : " + employees.size());
	}

	private static void asClause(Session session) {
		Query query = session.createQuery("FROM com.example.hibernate.program.Employee AS e");
		List<Employee> employees = query.list();
		for (Employee employee : employees) {
			System.out.println(employee);
		}
		System.out.println("Total Count : " + employees.size());
	}

	private static void selectClause(Session session) {
		Query query = session.createQuery("SELECT e.firstName, e.lastName, e.email FROM Employee AS e");
		List<Object[]> list = query.list();
		for (Object[] objects : list) {
			System.out.println(objects[0] + " " + objects[1] + " " + objects[2]);
		}
	}

	private static void whereClause(Session session) {
		Query query = session.createQuery("FROM Employee AS e WHERE e.id > 5");
		List<Employee> list = query.list();
		for (Employee employee : list) {
			System.out.println(employee);
		}
		System.out.println("Total : " + list.size());
	}

	private static void orderByClause(Session session) {
		Query query = session.createQuery("FROM Employee AS e ORDER BY e.lastName DESC");
		List<Employee> list = query.list();
		for (Employee employee : list) {
			if (employee.getLastName().length() > 4 && employee.getLastName().length() < 8) {
				System.out.println(employee);
			}
		}
	}

	private static void groupByClause(Session session) {
		Query query = session.createQuery("SELECT SUM(e.salary), e.firstName FROM Employee AS e GROUP BY e.firstName");
		List<Object[]> list = query.list();
		for (Object[] objects : list) {
			System.out.println(Arrays.toString(objects));
		}
		System.out.println("Total : " + list.size());
	}

	private static void parameterBinding(Session session){
		Query query = session.createQuery("FROM Employee AS e WHERE e.firstName = :name");
		query.setParameter("name", "Lars");
		List<Employee> list = query.list();
		for (Employee employee : list) {
			System.out.println(employee);
		}
	}
	
	private static void paginationQuery(Session session){
		Query query = session.createQuery("FROM Employee");
		query.setFirstResult(10);
		query.setMaxResults(25);
		List<Employee> list = query.list();
		for (Employee employee : list) {
			System.out.println(employee);
			
		}
		System.out.println("total : " + list.size());
	}
	
	private static void uniqueValue(Session session){
		Query query = session.createQuery("FROM Employee AS e WHERE e.id = 3");
		Employee uniqueResult = (Employee) query.uniqueResult();
		System.out.println(uniqueResult);
	}
	
}






















